package com.demo.controller.wechat;

import com.demo.dao.Result;
import com.demo.pojo.wechat.WeMenu;
import com.demo.service.wechat.WeMenuService;
import com.demo.tree.WechatMenuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wechat/menu")
public class WeMenuController {
    @Autowired
    private WeMenuService weMenuService;

    @PostMapping
    public Result addMenu(@RequestBody WeMenu menu) {
        weMenuService.save(menu);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteMenu(@RequestParam int id) {
        weMenuService.removeById(id);
        return Result.success();
    }

    @PutMapping
    public Result updateMenu(@RequestBody WeMenu menu) {
        weMenuService.updateById(menu);
        return Result.success();
    }

    @GetMapping
    private Result<List<WeMenu>> getMenuList() {
        List<WeMenu> list = weMenuService.lambdaQuery().select().list();
        WechatMenuTree wechatMenuTree = new WechatMenuTree();
        List<WeMenu> tree = wechatMenuTree.createTree(list, 0);
        return Result.success(tree);
    }
}
